package com.example.yiparkapplication.activity.index.activity.myCoupons;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.yiparkapplication.R;
import com.example.yiparkapplication.entity.MonthCard;

import java.text.SimpleDateFormat;
import java.util.List;

public class MyCouponsAdepter extends RecyclerView.Adapter<MyCouponsAdepter.ViewHolder> {
    private MyCouponsActivity mMyCouponsActivity;
    private List<MonthCard> mMonthCardList;
    private String mSearchType;
    private static String mUsableStreet = null;

    static class ViewHolder extends RecyclerView.ViewHolder{
        View receiveCouponView;
        TextView couponName;
        TextView couponTypeUnit;
        TextView couponType;
        TextView preferentialValues;
        TextView startTime;
        TextView endTime;
        Button receiveButton;
        TextView unusableReason;
        LinearLayout unusableReasonLinearLayout;
        TextView usableTimes;
        TextView usableStatue;
        LinearLayout couponInfo;
        TextView usableStreet;
        ImageView image;
        LinearLayout accessLimitLinearLayout;
        TextView accessLimit;

        public ViewHolder(View view){
            super(view);
            receiveCouponView = view;
            couponName = view.findViewById(R.id.id_coupon_name);
            couponTypeUnit = view.findViewById(R.id.id_coupon_type_unit);
            couponType = view.findViewById(R.id.id_coupon_type);
            preferentialValues = view.findViewById(R.id.id_preferential_values);
            startTime = view.findViewById(R.id.id_start_time);
            endTime = view.findViewById(R.id.id_end_time);
            usableTimes = view.findViewById(R.id.id_usable_times);
            receiveButton = view.findViewById(R.id.id_receive);
            unusableReasonLinearLayout = view.findViewById(R.id.id_unusable_reason_linearLayout);
            unusableReason = view.findViewById(R.id.id_unusable_reason);
            usableStatue = view.findViewById(R.id.id_usable_statue);
            couponInfo = view.findViewById(R.id.id_coupon_usable_info);
            usableStreet = view.findViewById(R.id.id_usable_street);
            image = view.findViewById(R.id.id_up_or_down_image);
            accessLimitLinearLayout = view.findViewById(R.id.id_access_limit_linearLayout);
            accessLimit = view.findViewById(R.id.id_access_limit);

        }
    }

    public MyCouponsAdepter(List<MonthCard> monthcard, MyCouponsActivity myCouponsActivity, String searchType){
        mMonthCardList = monthcard;
        mMyCouponsActivity = myCouponsActivity;
        mSearchType = searchType;
    }

    @Override
    public MyCouponsAdepter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType){
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_my_coupons_list_layout, parent, false);
        final MyCouponsAdepter.ViewHolder holder = new MyCouponsAdepter.ViewHolder(view);
        holder.receiveButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                int position = holder.getAdapterPosition();
                String couponId = mMonthCardList.get(position).getCouponId().toString();
                String monthCardName = mMonthCardList.get(position).getMonthCardName();
                int monthDiscountType = mMonthCardList.get(position).getMonthCardType();
                double monthDiscountValue = mMonthCardList.get(position).getMonthDiscountValue();
                mMyCouponsActivity.postCouponInfo(monthCardName, couponId, monthDiscountType, monthDiscountValue);
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyCouponsAdepter.ViewHolder holder, int position){
        final MonthCard monthCard = mMonthCardList.get(position);
        if ("cardBag".equals(mSearchType)){
            holder.receiveButton.setVisibility(View.GONE);
        }else if ("CarOrder".equals(mSearchType)){
            if (monthCard.getWhether() == null){
                holder.receiveButton.setText("使用");
                holder.usableStatue.setText("可使用");
            }else{
                holder.receiveButton.setVisibility(View.GONE);
                holder.unusableReasonLinearLayout.setVisibility(View.VISIBLE);
                holder.unusableReason.setText(monthCard.getReason());
                holder.usableStatue.setText("不可使用");
            }
        }
        holder.couponName.setText(monthCard.getMonthCardName());
        if (monthCard.getMonthDiscountType() == 0){
            holder.couponTypeUnit.setText("%");
            holder.couponType.setText("打折券");
            holder.preferentialValues.setText(String.valueOf((int)(monthCard.getMonthDiscountValue()*100)));
        }else{
            holder.couponTypeUnit.setText("元");
            holder.couponType.setText("抵用券");
            holder.preferentialValues.setText(String.valueOf(monthCard.getMonthDiscountValue()));
        }
        if (monthCard.getAccessLimit() > 0){
            holder.accessLimitLinearLayout.setVisibility(View.VISIBLE);
            holder.accessLimit.setText(String.valueOf(monthCard.getAccessLimit()));
        }
        if (monthCard.getStartTime() == null || "".equals(monthCard.getStartTime())){
            holder.receiveCouponView.findViewById(R.id.id_start_time_and_end_time).setVisibility(View.GONE);
        }else{
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            long startTime = Long.valueOf(monthCard.getStartTime());
            long endTime = Long.valueOf(monthCard.getEndTime());
            holder.startTime.setText(simpleDateFormat.format(startTime) + "至");
            holder.endTime.setText(simpleDateFormat.format(endTime));
        }
        if (monthCard.getUsableTimes() < 0){
            holder.usableTimes.setText("不限次数");
        }else{
            holder.usableTimes.setText(String.valueOf(monthCard.getUsableTimes()));
        }
        holder.couponInfo.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                if (holder.usableStreet.getVisibility() == View.GONE){
                    if (mMyCouponsActivity != null){
                        mUsableStreet = monthCard.getStreetName();
                    }
                    holder.usableStreet.setText("使用路段" + mUsableStreet);
                    holder.image.setBackgroundResource(R.drawable.pull_up);
                    holder.usableStreet.setVisibility(View.VISIBLE);
                }else{
                    holder.usableStreet.setVisibility(View.GONE);
                    holder.image.setBackgroundResource(R.drawable.pull_down);
                }
            }
        });
    }

    @Override
    public int getItemCount(){
        return mMonthCardList.size();
    }

}
